package com.luoran.bartalks.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.luoran.bartalks.action.vo.RuleRecordVo;
import com.luoran.bartalks.entity.biz.BtRuleRecord;
import com.luoran.bartalks.service.IBtRuleRecordPicService;
import com.luoran.bartalks.service.IBtRuleRecordService;

/**
 * 打尺记录的Controller
 * 
 * @author ghl 2018年1月15日 下午1:23:15
 */
@RequestMapping("/ruleRecord")
@Controller
public class BtRuleRecordAction {

    @Autowired
    private IBtRuleRecordService ruleRecordService;

    @Autowired
    private IBtRuleRecordPicService ruleRecordPicService;

    private static final Logger log = LoggerFactory.getLogger(BtRuleRecordAction.class);

    // 根据箱单物料关系id 获取物料打尺记录
    @RequestMapping("/getRuleRecord")
    @ResponseBody
    public JSONObject getRuleRecord(String id) {// 箱单物料关系id.
        log.info("参数[{}]", id);
        JSONObject result = new JSONObject();
        List<RuleRecordVo> recordVos = new ArrayList<>();
        try {
            List<BtRuleRecord> ruleRecord = ruleRecordService.getRuleRecord(id);
            for (BtRuleRecord btRuleRecord : ruleRecord) {
                RuleRecordVo recordVo = new RuleRecordVo();
                recordVo.setRuleId(btRuleRecord.getId());
                recordVo.setPositionName(btRuleRecord.getPositionName());
                recordVo.setRuleDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(btRuleRecord.getRuleDate()));
                recordVo.setLength(btRuleRecord.getLength());
                recordVo.setWidth(btRuleRecord.getWidth());
                recordVo.setHeight(btRuleRecord.getHeight());
                recordVo.setWeight(btRuleRecord.getWeight());
                recordVo.setDescription(btRuleRecord.getDescription());
                List<String> list = ruleRecordPicService.getPics(btRuleRecord.getId());
                recordVo.setPicList(list);
                recordVos.add(recordVo);
            }
            if (recordVos.size() != 0) {
                result.put("flag", true);
                result.put("msg", recordVos);
                return result;
            } else {
                result.put("flag", false);
                result.put("msg", "未找到相应的记录!");
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("flag", false);
            result.put("msg", "系统异常");
            return result;
        }

    }
}
